using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace TowerDefense_Dx.Core
{
    public class DxTimer
    {
        [DllImport("kernel32")]
        private static extern bool QueryPerformanceFrequency(ref long frequency);
        [DllImport("kernel32")]
        private static extern bool QueryPerformanceCounter(ref long count);

        private static long _lastTime = 0;
        private static long _currentTime = 0;
        private static long _tickPerSec = 0;
        private static double _ellapseMiliSec = 0.0;
        private static double _ellapseSec = 0.0;
        private static bool _isInit = false;
        public static long TickPerSec
        {
            get { return _tickPerSec;}
        }
         public static void Init()
         {
             if(!QueryPerformanceFrequency(ref _tickPerSec))
             {
                 throw new Exception("Performance Counter not supported");
             }
             _isInit = true;
         }
         public static void Start()
         {
             if(!_isInit)
             {
                 throw new Exception("Timer not initialized");
             }
             QueryPerformanceCounter(ref _lastTime);
         }
        public static double GetEllapseMilliSec()
        {
            if(!_isInit)
            {
                throw new Exception("Timeer not initialized");
            }
            QueryPerformanceCounter(ref _currentTime);
            _ellapseMiliSec = ((double)(_currentTime - _lastTime) /(double)_tickPerSec) *1000.0;
            _lastTime = _currentTime;
            return _ellapseMiliSec;
        }
        public static void Reset()
        {
            QueryPerformanceCounter(ref _lastTime);
        }
    }
}
